using System;
using System.Diagnostics;
using System.IO;
using UnityEngine;

namespace VoxelEngine
{
	public static class WorldSaveFileController
	{
		private const string WorldPersistentDirectoryRelativePath = "/Games/VoxelGame/";

		private const string RelativeDirectoryPath = "/PKGames/MiniGames/VoxelGame/Packs/Embedded/Data/";

		private const string WorldPersistentTempFilePrefix = "temp";

		private const string WorldFileExtension = "dat";

		private const string WorldEmbeddedFileExtension = "bytes";

		private const string EditorDirectoryPath = "Assets";

		public static void SaveWorld(string id, byte[] worldInBytes, int worldVisibleSizeX, int worldVisibleSizeY, int worldVisibleSizeZ, bool useDataExtension, bool isEditor = false)
		{
			if (!Directory.Exists(DirectoryPath(isEditor)))
			{
				Directory.CreateDirectory(DirectoryPath(isEditor));
			}
			Stopwatch stopwatch = new Stopwatch();
			string text = FullTempFilePath(id, isEditor);
			string text2 = FullFilePath(id, isEditor, useDataExtension);
			FileInfo fileInfo = new FileInfo(text);
			using (FileStream fileStream = fileInfo.OpenWrite())
			{
				byte[] bytes = BitConverter.GetBytes(worldVisibleSizeX);
				byte[] bytes2 = BitConverter.GetBytes(worldVisibleSizeY);
				byte[] bytes3 = BitConverter.GetBytes(worldVisibleSizeZ);
				fileStream.Write(bytes, 0, bytes.Length);
				fileStream.Write(bytes2, 0, bytes2.Length);
				fileStream.Write(bytes3, 0, bytes3.Length);
				stopwatch.Start();
				stopwatch.Stop();
				UnityEngine.Debug.Log("[Voxel] World converted to bytes in: " + stopwatch.ElapsedMilliseconds + " ms");
				stopwatch.Reset();
				stopwatch.Start();
				fileStream.Write(worldInBytes, 0, worldInBytes.Length);
			}
			stopwatch.Stop();
			UnityEngine.Debug.Log("[Voxel] World file write in: " + stopwatch.ElapsedMilliseconds + " ms");
			stopwatch.Reset();
			stopwatch.Start();
			if (File.Exists(text2))
			{
				File.Delete(text2);
			}
			File.Move(text, text2);
			stopwatch.Stop();
			UnityEngine.Debug.Log("[Voxel] World file moved in: " + stopwatch.ElapsedMilliseconds + " ms");
		}

		private static string FullTempFilePath(string id, bool isEditor)
		{
			return DirectoryPath(isEditor) + TempFileName(id);
		}

		private static string FullFilePath(string id, bool isEditor, bool useDataExtension)
		{
			return DirectoryPath(isEditor) + FileName(id, useDataExtension);
		}

		private static string DirectoryPath(bool isEditor)
		{
			return Application.persistentDataPath + ((!isEditor) ? "/Games/VoxelGame/" : "/PKGames/MiniGames/VoxelGame/Packs/Embedded/Data/");
		}

		private static string TempFileName(string id)
		{
			return string.Format("{0}_{1}.{2}", "temp", id, "dat");
		}

		private static string FileName(string id, bool useDataExtension)
		{
			return string.Format("{0}.{1}", id, (!useDataExtension) ? "bytes" : "dat");
		}

		public static void DeleteLocalSave(string id)
		{
			string path = FullFilePath(id, isEditor: false, useDataExtension: true);
			if (File.Exists(path))
			{
				File.Delete(path);
			}
		}
	}
}
